/**  
 * @date 2023-12-06 
 * @author Zhang Yu
 */
package com.springboot.template.service;

import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class RedisService{

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    public void setKey(String key, Object value) {
        redisTemplate.opsForValue().set(key, value);
    }

    public Object getKey(String key) {
        return redisTemplate.opsForValue().get(key);
    }
    
    public String setToken(String key) {
    	String redisKey = "token::" + key;
    	String value = fetchDataFromDatabase();
        redisTemplate.opsForValue().set(redisKey, value);
        return value;
    }
    
    public String getToken(String key) {
    	String redisKey = "token::" + key;
    	Object reObject = getKey(redisKey);
    	if (reObject instanceof String) {
    		return reObject.toString();
		}
		return "";
	}
    
    private String fetchDataFromDatabase() {
        UUID uuid = UUID.randomUUID();
        System.out.println(uuid);
        // 实际从数据库获取数据的逻辑
        return uuid.toString();
    }
}
